package mainWindowSupport;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import actionHandlers.ShapeTreeSelectListener;
import cuShapes.*;
import java.io.Serializable;

public class ShapeTree implements ShapeListObserver, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6857159505346815043L;
	private JTree shapeTree;
	private DefaultMutableTreeNode shapesRoot;
	
	public ShapeTree() {
		shapesRoot = new DefaultMutableTreeNode("Shapes",true);
		shapeTree = new JTree(shapesRoot);
		shapeTree.updateUI();
		shapeTree.addMouseListener(new ShapeTreeSelectListener());
	}
	
	public JTree getTree() {
		return shapeTree;
	}
	
	public void formTreeFromList(ShapeList nodes) {
		DefaultMutableTreeNode node = null;
		shapesRoot.removeAllChildren();
		for(PQShape x : nodes) {
			node = new DefaultMutableTreeNode(x);
			shapesRoot.add(node);
		}
		shapeTree.updateUI();
	}

	public void update(ShapeList s) {
		formTreeFromList(s);
	}
}